package me.maiz.tool.codegen.controller;


import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import me.maiz.tool.codegen.domain.UserDO;
import me.maiz.tool.codegen.service.UserService;
import me.maiz.tool.codegen.common.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-09-28 17:41:54 | Aron</small>
 */
@Controller
@RequestMapping("/codegen/user")
public class UserController extends AdminBaseController {
	@Autowired
	private UserService userService;
	
	@GetMapping()
	String User(){
	    return "/user/user";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public Result<Page<UserDO>> list(UserDO userDTO){
        Wrapper<UserDO> wrapper = new EntityWrapper<UserDO>().orderBy("userId", false);
        Page<UserDO> page = userService.selectPage(getPage(UserDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	String add(){
	    return "user/add";
	}

	@GetMapping("/edit/{userId}")
	String edit(@PathVariable("userId") Integer userId,Model model){
		UserDO user = userService.selectById(userId);
		model.addAttribute("user", user);
	    return "user/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public Result<String> save( UserDO user){
		userService.insert(user);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Result<String>  update( UserDO user){
		boolean update = userService.updateById(user);
		return update ? Result.ok() : Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public Result<String>  remove( Integer userId){
		userService.deleteById(userId);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public Result<String>  remove(@RequestParam("ids[]") Integer[] userIds){
		userService.deleteBatchIds(Arrays.asList(userIds));
		return Result.ok();
	}
	
}
